package com.login.dao.impl;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.login.bean.Buyer;
import com.login.dao.IBuyerDao;
import com.login.utils.DbUtils;

public class BuyerDaoImpl implements IBuyerDao {	
	@Override
	//用户登录

	public boolean BuyerLogin(String username, String password) {
		String sql="select * from tb_buyer where name=? and  passWord=?";		
		PreparedStatement ps=DbUtils.executePrepareStetement(sql);		
		try {		
			ps.setString(1, username);
			ps.setString(2, password);
			ResultSet rs=ps.executeQuery();				
			while(rs.next()){		
			 if(rs.getString("name").equals(username)&&rs.getString("passWord").equals(password))
				{
					return true;				
				}
				
				} 
			}catch (SQLException e) {
			
			e.printStackTrace();
		}
		DbUtils.close();	
		
		return false;
	}

	

	@Override
	public int saveBuyer(Buyer user, String time) {
		int result=0;
		String sql="insert into tb_buyer(name,passWord,identify,phoneNumber,registTime) values(?,?,?,?,?)";
		PreparedStatement ps=DbUtils.executePrepareStetement(sql);
		try {
			ps.setString(1, user.getName());
			ps.setString(2, user.getPwd());
			ps.setString(3, user.getIdentify());
			ps.setString(4, user.getPhoneNumber());
			ps.setString(5, time);
			result=ps.executeUpdate();
		} catch (SQLException e) {			
			e.printStackTrace();
		}
		return result;
	}

	@Override
	public int deleteBuyer(String username) {
		String sql="delete from tb_buyer where name=?";
		PreparedStatement ps=DbUtils.executePrepareStetement(sql);
		int result=0;
		try {
			ps.setString(1, username);
			result = ps.executeUpdate();
		} catch (SQLException e) {
			
			e.printStackTrace();
		}
		return result;
	}
	
	public int updateBuyer(String userpwd,String name) {
		String sql="update tb_buyer set passWord =? where name = ?";
		PreparedStatement ps=DbUtils.executePrepareStetement(sql);
		int result = 0;
		try {
			ps.setString(1, userpwd);		
			ps.setString(2, name);
			result =ps.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return result;
	}

	public List<Buyer>findAll(){
		String sql="select * from tb_buyer";				
		List<Buyer> list =new ArrayList<Buyer>();
		ResultSet rs=DbUtils.executeQuery(sql);
		try {
			while(rs.next()){
				Buyer user = new Buyer();
				user.setName(rs.getString("name"));
				user.setPwd(rs.getString("password"));
				user.setIdentify(rs.getString("identify"));
				user.setPhoneNumber(rs.getString("phoneNumber"));
				user.setRegistTime(rs.getString("registTime"));
				list.add(user);
			}
		} catch (SQLException e) {
			
			e.printStackTrace();
		}
		
		return list;
	}
	

	@Override
	public List<Buyer> findByUsername(String username) {
		String sql="select * from tb_buyer where name like '%"+username+"%'" ;
		ResultSet rs=DbUtils.executeQuery(sql);
		List<Buyer> list=new ArrayList<Buyer>();
		try {	
			if(rs.next()){
				Buyer user=new Buyer();
				user.setIdentify(rs.getString("identify"));
				user.setName(rs.getString("name"));
				user.setPhoneNumber(rs.getString("phoneNumber"));
				user.setPwd(rs.getString("passWord"));
				user.setAddress(rs.getString("address"));
				user.setContactNumber(rs.getString("contactNumber"));
				user.setRealName(rs.getString("realName"));
				user.setCarId(rs.getString("carId"));
				list.add(user);
			}
		} catch (SQLException e) {
			
			e.printStackTrace();
		}
		
		return list;
	}

	

	@Override
	public int saveBuyerInformation(String address, String contactNumber, String realName,Buyer user) {
		int result=0;
		String sql="update tb_buyer set address=?,contactNumber=?,realName=? where name=?";
		PreparedStatement ps=DbUtils.executePrepareStetement(sql);
		try {
			ps.setString(1, address);
			ps.setString(2, contactNumber);
			ps.setString(3, realName);	
			ps.setString(4, user.getName());
			result=ps.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;
	}


}
